<?php
/*
 * Template pour la liste des defis
 * Créer le 10-03-2013
 */
global $wpdb;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
/* Requête pour aller chercher les post-type des defis */
if( isset($_GET['projet']) && is_numeric($_GET['projet']) ) { $addReq = $_GET['projet']; }
$args = array( 
    'post_type'         => 'defi', 
    'posts_per_page'    => 8, 
    'post_status'       => 'publish', 
    'paged'             => $paged,
    'orderby'           => 'post_date',
    'order'             => 'DESC',
    'meta_value'        => $addReq
    );
$wp_query = null;
$wp_query = new WP_Query( $args );
   
// Compte le nombre de défis
$countNbDefi = wp_count_posts('defi')->publish;

get_header();

?>
<div class="content" role="main">

<?php    
    if ( $wp_query->have_posts() ) :  
        
        /*
         * AFFICHAGE DE LA MAP 
         */
        if($paramWpspoSetting['affmapsdefis']==1 && $countNbDefi>=1 && get_query_var('paged')==1) {
            
            $listeDefis = WPSPO_Defis::listForMap();
            //print_r($listeDefis);
            foreach ($listeDefis as $mapdefi) {
                
                $optionsMaps = WPSPO_Defis::get_options($mapdefi->ID);
                $valueOptions = json_decode($optionsMaps->defis_option_value);
                $coordDefis = $valueOptions->coord_defis;
                $recupLatLngDefi = explode("##", $coordDefis);
                $latDefis = $recupLatLngDefi[0];
                $lngDefis = $recupLatLngDefi[1];
                $projetStatus = get_post_status( $optionsMaps->defis_id_projet ); // Récupère le status du post
                if($latDefis!='' && $projetStatus=='publish') {
                    $listForMap .=  "
                ['<i><b>".get_the_title(WPSPO_ID_PROJETS).":</b><br /><a href=\"".get_permalink($optionsMaps->defis_id_projet)."\">".get_the_title($optionsMaps->defis_id_projet)."</a></i><br /><br /><strong>".$mapdefi->post_title."</strong><br />".addslashes(trim($mapdefi->post_excerpt))."<br /><br /><strong>".__('Date of challenge', 'sponsorpress').": </strong>".wpspo_display_date($valueOptions->date_defis, $valueOptions->date_end_defis)."<br /><strong>".__('City', 'sponsorpress').":</strong> ".addslashes(trim($valueOptions->ville_defis))."<br /><br /><div style=\"text-align:center; background-color:#E15821;margin:2px;\"><a href=\"".get_permalink($mapdefi->ID)."\" style=\"color:#fff;\"><strong>".__( 'See this challenge' , 'sponsorpress')."</strong></a><br />', ".$latDefis.", ".$lngDefis."],";
                }
            }
 ?>         <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
    <div class="post-inner">
        <header class="entry-header">
            <h1 class="name post-title entry-title entry-title"><?php _e('Map challenges', 'sponsorpress'); ?></h1>
        </header>
            <script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
            <div id="map" style="width: 100%; height: 550px;"></div>
            <?php echo wpspobutton_print( __( 'Organize a challenge' , 'sponsorpress'), 170, 'orange', get_permalink(WPSPO_ID_ORGANISER) ); ?>
            </div>
    </article>
    <div class="clear espace10"></div>
            <script type="text/javascript">
            <!--
                var locations = [
                 <?php echo $listForMap; ?>
                ];

                var image = '<?php echo WPSPO_IMG_PATH; ?>map24/pin2.png';

                var map = new google.maps.Map(document.getElementById('map'), {
                  zoom: 5,
                  center: new google.maps.LatLng(47.4,1.6),
                      mapTypeControl: true,
                    mapTypeControlOptions: {
                  style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                navigationControl: true,
                 navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL,
                    position: google.maps.ControlPosition.TOP_RIGHT
                },
                    scaleControl: true,
                streetViewControl: false,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var infowindow = new google.maps.InfoWindow();

                var marker, i;

                for (i = 0; i < locations.length; i++) {  
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map,
                    icon: image
                });

                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
                 }
            -->
              </script>
<?php
        }
        
         /*
         * AFFICHAGE DE LA LISTE DES DEFIS
         */
        while ( $wp_query->have_posts() ) : $wp_query->the_post();
    
            $data = WPSPO_defis::get_options(get_the_id());
            $valueOptions = json_decode($data->defis_option_value);
            //$countDefi = WPSPO_Defis::get_countByProjet(get_the_id());
            // Compte de nombre de promesse / participant
            /* Nombre de participant pour ce défi */
            $countNbParticipant = WPSPO_Users::get_count(get_the_id());
            $projetStatus = get_post_status( $data->defis_id_projet ); // Récupère le status du post
            //print_r($data);
            
            if($projetStatus=='publish') {
?>            
            <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
                <div class="post-inner">
                    <header class="entry-header">
                        <h1 class="name post-title entry-title entry-title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h1>
                    </header> 
                    <div class="entry-content">
                    <?php

                       if( has_post_thumbnail() ) { ?>


                      <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('thumbnail', array('class' => "alignleft wp-post-image")); ?></a>

                      <?php
                      } else {
                          // Si l'article n'a pas d'Image à la Une, on récupère la 1ère image attachée
                          $attachments = get_children(
                              array(
                              'post_parent' => get_the_ID(),
                              'post_type' => 'attachment',
                              'post_mime_type' => 'image',
                              'orderby' => 'menu_order',
                              'order' => 'DESC',
                              'numberposts' => 1
                              )
                          );
                          if( $attachments ) {
                              foreach( $attachments as $attachment ) :
                              ?>
                              <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php echo wp_get_attachment_image($attachment->ID , 'thumbnail', '', array('class' => "alignleft wp-post-image")); ?></a>

                              <?php
                              endforeach;
                          }
                      }
                    ?>
                    <div style="margin-top:10px;">
                        <strong><?php _e('Summary of the challenge:', 'sponsorpress'); ?></strong> <?php the_excerpt(); ?>
                    </div>
                    <div style="margin-top:10px;">
                        <strong><?php _e('Date of challenge:', 'sponsorpress'); ?></strong> <?php echo wpspo_display_date($valueOptions->date_defis, $valueOptions->date_end_defis); ?><br /><strong><?php _e('City:', 'sponsorpress'); ?></strong> <?php echo $valueOptions->ville_defis; ?>
                    </div>
                    <div style="margin-top:10px;">
                        <strong><?php _e('Number of contributor:', 'sponsorpress'); ?></strong> <?php echo $countNbParticipant; ?>
                    </div>
                    <div class="clear"></div>
                    <?php echo wpspobutton_print(__( 'See this challenge' , 'sponsorpress'), 170, 'vert'); ?>
                    </div>
                </div>
            </article>
            <div class="clear espace10"></div>
          <?php  
          
                
            }
            
        endwhile;

        // Pagination perso
        wpspo_theme_pagination();
                
        
    else :
        
    ?>
         <article <?php if( !empty( $rv['review'] ) ) echo $rv['review']; post_class('post-listing'); ?>>
            <div class="post-inner">
                <header class="entry-header">
                    <h1 class="name post-title entry-title entry-title"><?php _e( 'No challenge has been organized!' , 'sponsorpress'); ?></h1>
                </header>
                <?php
        echo '<div class="entry-content">'.sprintf(__( 'Select a <a href="%s">project</a> and organize a sporting challenge for you, your organization or your youth group!' , 'sponsorpress'), get_permalink(WPSPO_ID_PROJETS)).'';
        echo  wpspobutton_print(__( 'Discover all projects' , 'sponsorpress'), '250', 'orange', get_permalink(WPSPO_ID_PROJETS));
        echo '<div class="clear"></div></div>';
        echo '</div></article>';

    endif;
?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>